package m.jclecai.com.ui;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.util.List;

import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.OpenGradeBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.inter.OnResultCallback;
import m.jclecai.com.utils.OkHttpClientManager;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;

/**
 * Created by hp on 2017/3/17.
 * <p>
 * 胜负彩详情
 */

public class SFC_LotteryDetails extends BaseActivity implements View.OnClickListener {

    private TextView tv_title;
    private TextView tv_title_right;
    //全国销量
    private TextView tv_totalSales;
    //奖池滚存
    private TextView tv_bonusPool;
    private TextView tv_nums_sfc;
    private TextView tv_prize_sfc;
    private TextView tv_nums_sfc_two;
    private TextView tv_prize_sfc_two;
    private TextView[] guestTests;
    private TextView[] homeTests;
    private TextView[] codeTests;
    private String type;


    @Override
    protected int getContentView() {
        return R.layout.sfc_lottery_details;
    }

    @Override
    protected void initView() {
        setTitle("开奖详情");
        rightOptionText("开奖历史");
        leftOptionText("");

        TextView tv_back = (TextView) findViewById(R.id.tv_back);
        tv_back.setOnClickListener(this);


        Button bt_bottom_left = (Button) findViewById(R.id.bt_bottom_left);
        bt_bottom_left.setText("胜负彩投注");
        bt_bottom_left.setOnClickListener(this);

        tv_bonusPool = (TextView) findViewById(R.id.tv_bonusPool);
        tv_totalSales = (TextView) findViewById(R.id.tv_totalSales);

        tv_nums_sfc = (TextView) findViewById(R.id.tv_nums_sfc);
        tv_prize_sfc = (TextView) findViewById(R.id.tv_prize_sfc);

        tv_nums_sfc_two = (TextView) findViewById(R.id.tv_nums_sfc_two);
        tv_prize_sfc_two = (TextView) findViewById(R.id.tv_prize_sfc_two);

        initTabView();

    }

    @Override
    protected void resetWindow() {
    }

    private void initTabView() {
        TextView tv_home_one = (TextView) findViewById(R.id.tv_home_one);
        TextView tv_home_two = (TextView) findViewById(R.id.tv_home_two);
        TextView tv_home_three = (TextView) findViewById(R.id.tv_home_three);
        TextView tv_home_four = (TextView) findViewById(R.id.tv_home_four);
        TextView tv_home_five = (TextView) findViewById(R.id.tv_home_five);
        TextView tv_home_six = (TextView) findViewById(R.id.tv_home_six);
        TextView tv_home_seven = (TextView) findViewById(R.id.tv_home_seven);
        TextView tv_home_eight = (TextView) findViewById(R.id.tv_home_eight);
        TextView tv_home_nine = (TextView) findViewById(R.id.tv_home_nine);
        TextView tv_home_ten = (TextView) findViewById(R.id.tv_home_ten);
        TextView tv_home_eleven = (TextView) findViewById(R.id.tv_home_eleven);
        TextView tv_home_twelve = (TextView) findViewById(R.id.tv_home_twelve);
        TextView tv_home_thirteen = (TextView) findViewById(R.id.tv_home_thirteen);
        TextView tv_home_fourteen = (TextView) findViewById(R.id.tv_home_fourteen);

        homeTests = new TextView[]{tv_home_one, tv_home_two, tv_home_three, tv_home_four, tv_home_five, tv_home_six, tv_home_seven, tv_home_eight
                , tv_home_nine, tv_home_ten, tv_home_eleven, tv_home_twelve, tv_home_thirteen, tv_home_fourteen};

        TextView tv_guest_one = (TextView) findViewById(R.id.tv_guest_one);
        TextView tv_guest_two = (TextView) findViewById(R.id.tv_guest_two);
        TextView tv_guest_three = (TextView) findViewById(R.id.tv_guest_three);
        TextView tv_guest_four = (TextView) findViewById(R.id.tv_guest_four);
        TextView tv_guest_five = (TextView) findViewById(R.id.tv_guest_five);
        TextView tv_guest_six = (TextView) findViewById(R.id.tv_guest_six);
        TextView tv_guest_seven = (TextView) findViewById(R.id.tv_guest_seven);
        TextView tv_guest_eight = (TextView) findViewById(R.id.tv_guest_eight);
        TextView tv_guest_nine = (TextView) findViewById(R.id.tv_guest_nine);
        TextView tv_guest_ten = (TextView) findViewById(R.id.tv_guest_ten);
        TextView tv_guest_eleven = (TextView) findViewById(R.id.tv_guest_eleven);
        TextView tv_guest_twelve = (TextView) findViewById(R.id.tv_guest_twelve);
        TextView tv_guest_thirteen = (TextView) findViewById(R.id.tv_guest_thirteen);
        TextView tv_guest_fourteen = (TextView) findViewById(R.id.tv_guest_fourteen);

        guestTests = new TextView[]{tv_guest_one, tv_guest_two, tv_guest_three, tv_guest_four, tv_guest_five, tv_guest_six, tv_guest_seven, tv_guest_eight
                , tv_guest_nine, tv_guest_ten, tv_guest_eleven, tv_guest_twelve, tv_guest_thirteen, tv_guest_fourteen};

        TextView tv_code_one = (TextView) findViewById(R.id.tv_code_one);
        TextView tv_code_two = (TextView) findViewById(R.id.tv_code_two);
        TextView tv_code_three = (TextView) findViewById(R.id.tv_code_three);
        TextView tv_code_four = (TextView) findViewById(R.id.tv_code_four);
        TextView tv_code_five = (TextView) findViewById(R.id.tv_code_five);
        TextView tv_code_six = (TextView) findViewById(R.id.tv_code_six);
        TextView tv_code_seven = (TextView) findViewById(R.id.tv_code_seven);
        TextView tv_code_eight = (TextView) findViewById(R.id.tv_code_eight);
        TextView tv_code_nine = (TextView) findViewById(R.id.tv_code_nine);
        TextView tv_code_ten = (TextView) findViewById(R.id.tv_code_ten);
        TextView tv_code_eleven = (TextView) findViewById(R.id.tv_code_eleven);
        TextView tv_code_twelve = (TextView) findViewById(R.id.tv_code_twelve);
        TextView tv_code_thirteen = (TextView) findViewById(R.id.tv_code_thirteen);
        TextView tv_code_fourteen = (TextView) findViewById(R.id.tv_code_fourteen);

        codeTests = new TextView[]{tv_code_one, tv_code_two, tv_code_three, tv_code_four, tv_code_five, tv_code_six, tv_code_seven, tv_code_eight
                , tv_code_nine, tv_code_ten, tv_code_eleven, tv_code_twelve, tv_code_thirteen, tv_code_fourteen};
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");

        setTitle(intent.getStringExtra("typeName"));
        getOpenGradeData(intent.getStringExtra("term"), type);
    }

    private void getOpenGradeData(String term, String type) {
        showDialog();
        OkHttpClientManager.Param Url = new OkHttpClientManager.Param("url", Constant_Url.OPEN_GRADE);
        OkHttpClientManager.Param Term = new OkHttpClientManager.Param("term", term);
        OkHttpClientManager.Param Type = new OkHttpClientManager.Param("type", type);
        OkHttpClientManager.Param param[] = {Url, Term, Type};
        try {
            OkHttpClientManager.getInstance().asynPost(new OnResultCallback() {
                @Override
                public void onGetResult(String json) {
                    OpenGradeBean bean = FastOk.get(json, OpenGradeBean.class);
                    setViewInfos(bean);
                    dissDialog();
                }

                @Override
                public void onError() {
                    dissDialog();
                    ToastUtil.showToast(SFC_LotteryDetails.this, R.string.connect_failure);
                }
            }, param);
        } catch (IOException e) {
            dissDialog();
            e.printStackTrace();
        }
    }

    private void setViewInfos(OpenGradeBean bean) {
        if (bean == null) return;
        try {
            String[] code = bean.getCodes().split(",");
            List<OpenGradeBean.Teams> teams = bean.getTeams();

            for (int i = 0; i < homeTests.length; i++) {
                homeTests[i].setText(teams.get(i).getHomeName());
                guestTests[i].setText(teams.get(i).getGuestName());
                codeTests[i].setText(code[i]);
            }

            tv_bonusPool.setText(bean.getAllMoney());
            tv_totalSales.setText(bean.getNextPrize());

            OpenGradeBean.PrizeLevel prizeLevel = bean.getPrizeLevel();
            tv_nums_sfc.setText(prizeLevel.getNums().get(0));
            tv_prize_sfc.setText(prizeLevel.getPrize().get(0));

            tv_nums_sfc_two.setText(prizeLevel.getNums().get(1));
            tv_prize_sfc_two.setText(prizeLevel.getPrize().get(1));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @OnClick(R.id.tv_right_option)
    void toActivity() {
        Intent intent = new Intent(this, LotteryHistoryActivity.class);
        intent.putExtra("type", type);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                this.finish();
                break;
            case R.id.bt_bottom_left:
                Intent intent = new Intent(this, BetRXJ_SFCActivity.class);
                intent.putExtra("type", type);
                startActivity(intent);
                break;
        }
    }
}
